﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using EventReminder.Common;

namespace EventReminder.Server
{
    public class TaskEventSystem
    {
        public void BuildMessageSystem(EventBuilder eb)
        {
            eb.BuildData();
            eb.BuildList();
            eb.BuildDataToSend();

            //return eb;
        }
    }

    public abstract class EventBuilder
    {
        protected EventManager _event;

        public EventManager Event
        {
            get { return _event; }
        }

        public abstract void BuildData();
        public abstract void BuildList();
        public abstract void BuildDataToSend();
        public abstract List<string> DisplayAllData();
        public abstract void MarkMessagesToSend();
        public abstract List<string> GetMessagesToSend();
        public abstract void EventRefresh();
    }

    public class EventStart : EventBuilder
    {
        
        public EventStart()
        {
            _event = new EventManager();
        }

        public override void EventRefresh()
        {
            //throw new NotImplementedException();
            _event.RefreshEventList();
        }

        public override List<string> DisplayAllData()
        {

            List<string> returnedata = _event.DisplayAllEvents;

            return returnedata;
        }

        public override void MarkMessagesToSend()
        {
            _event.MarkMessagesToSend(1);
        }

        public override List<string> GetMessagesToSend()
        {
            List<string> returnedata = _event.GetMessagesToSend;

            return returnedata;
        }

        public override void BuildData()
        {
            try
            {
                using (StreamReader textIn = new StreamReader(
                    new FileStream(
                         @"..\..\..\data\eventrecords.txt", FileMode.OpenOrCreate, FileAccess.Read)))
                {
                    while (textIn.Peek() != -1)
                    {
                        string row = textIn.ReadLine();
                        EventData e = _event.CreateEvent(row);
                        _event.AddEvent(e);
                    }
                }
            }

            catch
            {

            }
        }

        public override void BuildList()
        {
            List<EventData> eventlist = new List<EventData>();

            eventlist = _event.GetMessageQueue(1);

            try
            {
                using (StreamWriter sw = new StreamWriter(
                    new FileStream(
                        @"..\..\..\Data\messagedata.txt", FileMode.Create, FileAccess.Write)))
                {
                    foreach (EventData e in eventlist)
                    {
                        sw.WriteLine(_event.WriteEvent(e));
                    }
                }
            }
            catch
            {
                
            }
        }

        public override void BuildDataToSend()
        {
            List<Users> userlist = new List<Users>();

            try
            {
                using (StreamReader textIn = new StreamReader(
                    new FileStream(
                        @"..\..\..\data\userinfo.txt", FileMode.Open, FileAccess.Read)))
                {
                    while (textIn.Peek() != -1)
                    {
                        string row = textIn.ReadLine();
                        Users u = CreateUser(row);
                        userlist.Add(u);
                    }
                }
            }

            catch
            {
                //MessageBox.Show("There was a problem with the file.", "Error");
            }

            foreach (Users u in userlist)
            {
                List<EventData> userlistdata = _event.GetUserMessage(u.GetUserID);

                if (userlistdata.Count > 0)
                {
                    try
                    {
                        string filename = @"..\..\..\Data\" + u.LastName + "_usermessagedata.txt";
                        using (StreamWriter sw = new StreamWriter(
                            new FileStream(
                               filename , FileMode.Create, FileAccess.Write)))
                        {
                            foreach (EventData e in userlistdata)
                            {
                                sw.WriteLine(_event.WriteEvent(e));
                            }
                        }
                    }
                    catch
                    {

                    }
                }
            }
        }

        /*
        private EventData CreateEvent(string data)
        {
            string[] dataline;
            dataline = data.Split('|');
            EventData e = new EventData();

            e.ID = Convert.ToInt32(dataline[0]);
            e.Date = Convert.ToDateTime(dataline[1]);
            e.Title = dataline[2];
            e.Description = dataline[3];
            string[] emaillist = dataline[4].Split(';');
            e.Emails = emaillist;
            e.Reminder = dataline[5];
            e.FollowUp = Convert.ToBoolean(dataline[6]);
            e.LastReminderSent = Convert.ToDateTime(dataline[7]);
            e.NextReminderDue = Convert.ToDateTime(dataline[8]);
            e.MessageOwner = Convert.ToInt32(dataline[9]);
            //e.UserFName = dataline[10];
            //e.UserLName = dataline[11];
            //e.UserEmail = dataline[12];
            return e;
        }
        */

        private Users CreateUser(string data)
        {
            string[] dataline;
            dataline = data.Split('|');
            Users u = new Users();

            u.GetUserID = Convert.ToInt32(dataline[0]);
            u.FirstName = dataline[1];
            u.LastName = dataline[2];
            u.UserEmail = dataline[3];
            return u;
        }

        
    }

    
}
